import json

from myapp import models
from myapp.utils import getPublicData


def getPageData():
    return list(getPublicData.educations.keys())




def getAverage(lists):
    num = 0
    for i in lists:
        num += i
    return round(num / len(lists), 2)


def getExperienceData(eduType):
    hasEmpty = False
    if eduType == '不限':
        jobs = models.JobInfo.objects.all()
    else:
        jobs = models.JobInfo.objects.filter(educational=eduType).all()
    if len(jobs) == 0: hasEmpty = True

    experineceData = {}
    peopleData = {}
    for i in getPublicData.workExperience:
        experineceData[i]= []
        peopleData[i] = 0

    for j in jobs:
        if j.pratice == 0:
            for k, v in experineceData.items():
                if k == j.workExperience:
                    experineceData[k].append(json.loads(j.salary)[1])
                    peopleData[k] += 1

    for k, v in experineceData.items():
        if len(v) == 0:
            experineceData[k] = 0
        else:
            experineceData[k] = getAverage(v)

    # return x轴, 右y轴, 左y轴
    return getPublicData.workExperience, list(experineceData.values()), list(peopleData.values()), hasEmpty


def getEducationalData():
    jobs = models.JobInfo.objects.all()

    data = {}
    for j in jobs:
        if data.get(j.educational, -1) == -1:
            data[j.educational] = 1
        else:
            data[j.educational] += 1

    return list(data.keys()), list(data.values())





